import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

//生成当前的日期,(年,月,日)
function getCurrentDate() {
  const date = new Date()
  return{
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    date: date.getDate(),
  }
}
//计算列表数组
/**
 * @param {number} year 
 * @param {number} month 
 * @returns {Array} 列表数组 [] 包括 [{type:'last,data:'i'}]
 */
function initList(year, month) {
    const list = []

  // 1. 计算选中的年月的1号星期几
  const firstDate = new Date(year, month - 1, 1)
  let day = firstDate.getDay() // 0-6
  if (day === 0) {
    day = 7
  }
  //计算公式为:等差数列  an = a0 + (n1-1)*1

  // 2. 填充上个月
  // 2.1 计算上个月的最后一天
  const lastMonthLastDate = new Date(year, month - 1, 0)
  const an = lastMonthLastDate.getDate()
  // 2.2 上个月的总天数
  const n1 = day - 1
  const a0 = an - n1 + 1
  for (let i = a0; i <= an; i++) {
    list.push({ type: 'last', date: i })
  }

  // 3. 填充本月
  const monthLastDate = new Date(year, month, 0)
  // 3.1 本月的最后一天
  const bn = monthLastDate.getDate()
  for (let i = 1; i <= bn; i++) {
    list.push({ type: 'this', date: i })
  }

  // 4. 填充下个月
  const cn = 42 - n1 - bn
  for (let i = 1; i <= cn; i++) {
    list.push({ type: 'next', date: i })
  }

  return list
}




export const useCounterStore = defineStore('calendar', () => {
  //当前日期
  const currentDate = getCurrentDate()
  //选中的日期
  const selectedDate = ref(currentDate)
  //选中的年
  const year = computed({
    get:()=>selectedDate.value.year,
    set:(value)=>{
      selectedDate.value.year = value
    }
  })
  //选中的月
  const month = computed({
    get:()=>selectedDate.value.month,
    set:(value)=>{
      selectedDate.value.month = value
    }
  })
  //选中的日
  const date = computed({
    get:()=>selectedDate.value.date,
    set:(value)=>{
      selectedDate.value.date = value
    }
  })
  //实现返回上个月
  const lastMonth = () => {
    //实现点击切换的时候,如果选中的date在最后一天,切换后仍然为最后一天的样式
      // 计算本月的天数
  const thisMonthDays = new Date(year.value, month.value, 0).getDate()
  // 计算上个月的天数
  const prevMonthDays = new Date(year.value, month.value - 1, 0).getDate()

  if (prevMonthDays < thisMonthDays) {
    date.value = prevMonthDays
  }


    month.value = month.value - 1
    if(month.value <=0){
      year.value--
      month.value = 12
    }
  }
  //实现返回下个月按钮
  const nextMonth = () => {
      // 计算本月的天数
  const thisMonthDays = new Date(year.value, month.value, 0).getDate()
  // 计算上个月的天数
  const nextMonthDays = new Date(year.value, month.value + 1, 0).getDate()

  if (nextMonthDays < thisMonthDays) {
    date.value = nextMonthDays
  }


    month.value = month.value + 1
    if(month.value > 12){
      year.value++
      month.value = 1
    }
  }
  //返回当前日期
  const backToToday = () => {
    selectedDate.value = getCurrentDate()
  }
  //计算数列
  const lists = computed(() => {
    return initList(year.value, month.value)
  })
  //底部组件返回格式化日期
    const formatDate = computed(() => {
    return `${year.value}-${month.value}-${date.value}`
  })
  return { 
    year,
    month,
    date,
    lists,
    formatDate,
    lastMonth,
    nextMonth,
    backToToday,
   }
})
